package com.x.erp.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.x.erp.model.IntelligentRobot;
import com.x.erp.model.SelectTemplate;
import com.x.erp.rpc.ClientCarRpcService;
import com.x.erp.rpc.IntelligentRobotRpcService;
import com.x.erp.rpc.SelectTemplateRpcService;
import com.x.framework.controller.BaseController;
import com.x.framework.controller.ResultCode;
import com.x.framework.controller.ResultModel;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * 大屏智能机器人信息
 */
@RestController
@RequestMapping("/robot")
public class IntelligentRobotController extends BaseController{

	@Reference
	private IntelligentRobotRpcService intelligentRobotRpcService;// 大屏智能机器人信息服务

	@Reference
	private SelectTemplateRpcService selectTemplateRpcService;// 大屏智能机器人信息服务

	@Reference
	private ClientCarRpcService clientCarRpcService;// C端车辆信息服务

	/**
	 * 查询大屏智能机器人信息列表
	 * @param
	 * @return
	 */
	@RequestMapping(value = "/getIntelligentRobotList", method = {RequestMethod.POST, RequestMethod.GET})
	public ResultModel getIntelligentRobotList(IntelligentRobot intelligentRobot) {
		return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功", intelligentRobotRpcService.selectIntelligentRobotList(intelligentRobot));
	}

	/**
	 * 查询大屏智能机器人信息数量
	 * @param
	 * @return
	 */
	@RequestMapping(value = "/getIntelligentRobotNum", method = {RequestMethod.POST, RequestMethod.GET})
	public ResultModel getIntelligentRobotNum(IntelligentRobot intelligentRobot) {
		return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功", intelligentRobotRpcService.selectIntelligentRobotNum(intelligentRobot));
	}

	/**
	 * 根据id查询
	 * @param intelligentRobot
	 * @return
	 */
	@RequestMapping(value = "/getIntelligentRobot", method = {RequestMethod.POST, RequestMethod.GET})
	public ResultModel getIntelligentRobot(IntelligentRobot intelligentRobot) {
		if(StringUtils.isEmpty(intelligentRobot.getRobotId())){
			return new ResultModel(ResultCode.RESULT_FAIL, "id不能为空！");
		}
		return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功", intelligentRobotRpcService.selectIntelligentRobot(intelligentRobot));
	}

	/**
	 * 新增大屏智能机器人信息
	 * @param
	 * @return
	 */
	@RequestMapping(value = "/insertIntelligentRobot", method = {RequestMethod.POST, RequestMethod.GET})
	public ResultModel insertIntelligentRobot(IntelligentRobot intelligentRobot, SelectTemplate selectTemplate) {
		if(intelligentRobot == null) {
			return new ResultModel(ResultCode.RESULT_FAIL, "大屏智能机器人信息不能为空！");
		}
		if(intelligentRobot.getRobotType() == null) {
			return new ResultModel(ResultCode.RESULT_FAIL, "类型不能为空！");
		}
		if(intelligentRobot.getRobotSeq() == null || intelligentRobot.getRobotSeq() < 1 || intelligentRobot.getRobotSeq() > 99){
			return new ResultModel(ResultCode.RESULT_FAIL, "排序值不能为空且在1~99之前！");
		}
		if(StringUtils.isEmpty(intelligentRobot.getRobotTitle())) {
			return new ResultModel(ResultCode.RESULT_FAIL, "标题不能为空！");
		}
		if(intelligentRobot.getRobotType() == 1 && StringUtils.isEmpty(intelligentRobot.getRobotContent())) {
			return new ResultModel(ResultCode.RESULT_FAIL, "内容不能为空！");
		}
		Date cDate = this.getDate(0);
		if(intelligentRobot.getRobotType() == 2) {
			if(selectTemplate == null || !selectTemplate.isNotNull()) {
				return new ResultModel(ResultCode.RESULT_FAIL, "条件不能为空！");
			}
			selectTemplate.setTemplateCreateTime(cDate);
			selectTemplate.setTemplateUpdateTime(cDate);
			intelligentRobot.setSelectTemplate(selectTemplate);
		}
		if(StringUtils.isEmpty(intelligentRobot.getUserId())){
			return new ResultModel(ResultCode.RESULT_FAIL, "用户id不能为空！");
		}
		intelligentRobot.setRobotState(1);
		intelligentRobot.setRobotCreateTime(cDate);
		intelligentRobot.setRobotUpdateTime(cDate);
		return new ResultModel(ResultCode.RESULT_SUCCESS, "插入成功！", intelligentRobotRpcService.insertIntelligentRobot(intelligentRobot));
	}

	/**
	 * 修改大屏智能机器人信息
	 * @param
	 * @return
	 */
	@RequestMapping(value = "/updateIntelligentRobot", method = {RequestMethod.POST, RequestMethod.GET})
	public ResultModel updateIntelligentRobot(IntelligentRobot intelligentRobot, SelectTemplate selectTemplate) {
		if(StringUtils.isEmpty(intelligentRobot.getRobotId())) {
			return new ResultModel(ResultCode.RESULT_FAIL, "ID不能为空！");
		}
		if(intelligentRobot.getRobotType() == null) {
			return new ResultModel(ResultCode.RESULT_FAIL, "类型不能为空！");
		}
		if(intelligentRobot.getRobotSeq() == null || intelligentRobot.getRobotSeq() < 1 || intelligentRobot.getRobotSeq() > 99){
			return new ResultModel(ResultCode.RESULT_FAIL, "排序值不能为空且在1~99之前！");
		}
		if(StringUtils.isEmpty(intelligentRobot.getRobotTitle())) {
			return new ResultModel(ResultCode.RESULT_FAIL, "标题不能为空！");
		}
		if(intelligentRobot.getRobotType() == 1 && StringUtils.isEmpty(intelligentRobot.getRobotContent())) {
			return new ResultModel(ResultCode.RESULT_FAIL, "内容不能为空！");
		}
		Date cDate = this.getDate(0);
		if(intelligentRobot.getRobotType() == 2) {
			if(selectTemplate == null || !selectTemplate.isNotNull()) {
				return new ResultModel(ResultCode.RESULT_FAIL, "条件不能为空！");
			}
			if(StringUtils.isEmpty(selectTemplate.getTemplateId())){
				selectTemplate.setTemplateCreateTime(cDate);
			}
			selectTemplate.setTemplateUpdateTime(cDate);
			intelligentRobot.setSelectTemplate(selectTemplate);
		}
		intelligentRobot.setRobotUpdateTime(cDate);
		intelligentRobotRpcService.updateIntelligentRobot(intelligentRobot);
		return new ResultModel(ResultCode.RESULT_SUCCESS, "修改成功！");
	}

	/**
	 * 删除大屏智能机器人信息
	 * @param
	 * @return
	 */
	@RequestMapping(value = "/deleteIntelligentRobot", method = {RequestMethod.POST, RequestMethod.GET})
	public ResultModel deleteIntelligentRobot(IntelligentRobot intelligentRobot) {
		if(StringUtils.isEmpty(intelligentRobot.getRobotId())) {
			return new ResultModel(ResultCode.RESULT_FAIL, "ID不能为空！");
		}
		intelligentRobotRpcService.deleteIntelligentRobot(intelligentRobot);
		return new ResultModel(ResultCode.RESULT_SUCCESS, "删除成功！");
	}
}
